<?php
 

/* Check if this is a valid include */
if (!defined('IN_SCRIPT')) {die($hdlang['attempt']);}

/***************************
Function hd_uploadFiles()
***************************/
function hd_uploadFile($i) {
global $hd_settings, $hdlang, $trackingID;

/* Return if name is empty */
if (empty($_FILES['attachment']['name'][$i])) {return '';}

/* Check file size */
if ($_FILES['attachment']['size'][$i] > ($hd_settings['attachments']['max_size']*1024)) {
    hd_error(sprintf($hdlang['file_too_large'],$_FILES['attachment']['name'][$i]));
} else {
    $file_size = $_FILES['attachment']['size'][$i];
}

/* Check file extension */
$ext = strtolower(strrchr($_FILES['attachment']['name'][$i], "."));
if (!in_array($ext,$hd_settings['attachments']['allowed_types'])) {
    hd_error(sprintf($hdlang['type_not_allowed'],$_FILES['attachment']['name'][$i]));
}

/* Generate a random file name */
$file_realname = str_replace(array('/','\\','#',',',' '), array('','','','','_'),$_FILES['attachment']['name'][$i]);
$useChars='AEUYBDGHJLMNPQRSTVWXZ123456789';
$tmp = $useChars{mt_rand(0,29)};
for($j=1;$j<10;$j++)
{
    $tmp .= $useChars{mt_rand(0,29)};
}
$file_name = substr($trackingID . '_' . md5($tmp . $file_realname), 0, 200) . $ext;

/* If upload was successful let's create the headers */
if (!move_uploaded_file($_FILES['attachment']['tmp_name'][$i], $hd_settings['server_path'].'/attachments/'.$file_name)) {
    hd_error($hdlang['cannot_move_tmp']);
}

$info = array(
    'saved_name'=> $file_name,
    'real_name' => $file_realname,
    'size'      => $file_size
);

return $info;
} // End hd_uploadFile()
?>
